package com.leoman.service.impl;


import com.leoman.dao.ImageDao;
import com.leoman.dao.QuestionDao;
import com.leoman.entity.Image;
import com.leoman.entity.Question;
import com.leoman.service.ImageService;
import com.leoman.service.UploadImageService;
import com.leoman.utils.BeanUtil;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.List;

/**
 * 微信图片处理
 * Created by 涂奕恒 on 14-10-16.
 */
@Service
@Transactional(readOnly = true)
public class ImageServiceImpl implements ImageService {

    @Autowired
    private ImageDao imageDao;

    @Autowired
    private UploadImageService uploadImageService;

    @Override
    public Image getById(Integer id) {
        return imageDao.findOne(id);
    }

    @Override
    @Transactional
    public Image deleteById(Integer id) {
        Image image = getById(id);
        imageDao.delete(image);
        return image;
    }

    @Override
    @Transactional
    public Image create(Image image) {
        return imageDao.save(image);
    }

    @Override
    @Transactional
    public Image createPlus(Image image, MultipartFile multipartFile) {
        WxCpService wxCpService = (WxCpService) BeanUtil.getBean("wxCpService1");
        try {
            // 上传为临时素材
            CommonsMultipartFile cf = (CommonsMultipartFile) multipartFile;
            DiskFileItem fi = (DiskFileItem) cf.getFileItem();
            File file = fi.getStoreLocation();
            WxMediaUploadResult result = wxCpService.mediaUpload(WxConsts.MEDIA_IMAGE, file);
            image.setMediaId(result.getMediaId());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        return imageDao.save(image);
    }

    @Override
    public List<Image> findListByQuestionId(Integer questionId) {
        return imageDao.findListByQuestionId(questionId);
    }

    @Override
    @Transactional
    public Integer batchAddImage(MultipartRequest multipartRequest, Integer questionId) {
        try {
            Integer count = 8;
            List<MultipartFile> list = multipartRequest.getFiles("fileselect[]");
            if (list.size() < count) {
                count = list.size();
            }
            Image image;
            Question question;
            for (int i = 0; i < count; i++) {
                if (null != list.get(i)) {
                    image = new Image();
                    question = new Question();
                    question.setId(questionId);
                    image.setQuestion(question);
                    image.setUrl(uploadImageService.saveImage(list.get(i)).getPath());

                    create(image);
                }
            }

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer batchAddImage(List<MultipartFile> imageList, Integer questionId) {
        try {
            Integer count = 8;
            if (imageList.size() < count) {
                count = imageList.size();
            }
            Image image;
            Question question;
            for (int i = 0; i < count; i++) {
                if (null != imageList.get(i)) {
                    image = new Image();
                    question = new Question();
                    question.setId(questionId);
                    question.setId(questionId);
                    image.setQuestion(question);
                    image.setUrl(uploadImageService.saveImage(imageList.get(i)).getPath());

                    create(image);
                }
            }

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
